<?php
namespace View;
class PageView {
  
  private $m_metaTags = array();
  private $m_charset;
  
  /**
  * Called when using new PageView();
  * @param string $charset  
  **/
  public function __construct($charset = "utf-8") {
    $this->m_charset = $charset;
  }
  
  /**
  * Adds a CSS stylesheet to the head of the document
  * @param urlstring $href url to css file
  **/
  public function AddStyleSheet($href) {
    //TODO: "/>" is not really valid for "HTML 4.01 Transitional" so these tags are not ended
    $this->m_metaTags[] = "<link rel='StyleSheet' href='$href' type='text/css'";
  }
  
  /**
   * Builds meta and CSS tags as a HTML/XML string
   *    
   * @param bool $isXML is the document an XML file and tags should be closed 
   * @return string  
   */
  private function BuildHeadTags($isXML) {
    $end = ">";
    if ($isXML) {
      $end = "/>";
    }
    $retValue = "";
    foreach($this->m_metaTags as $tag) {
      $retValue .= $tag . "$end\n            "; // "\n            " for readability
    }
    return $retValue;
  }
  
  /**
  * Returns a HTML 4.01 Transitional page
  * @param string $title  
  * @param string $body    
  * @return string     
  **/
  public function GetHTMLPage($title, $body, $login) {
    $link ="";
    $db="";
    $head = $this->BuildHeadTags(false);
    $loginView = new \Model\LoginHandler($db);
    if ($loginView->IsLoggedIn()==true)
    {
      $link = '<li><a href=index.php?page=newpost>Ny Post</a></li>';
      $link .= '<li><a href="TestAll.php">Tester</a></li>';
    }
        
    $html = "
        <!DOCTYPE HTML SYSTEM>
        <html>
          <head>
            <title>$title</title>
            <meta http-equiv='content-type' content='text/html; charset=$this->m_charset'>
            <link href='http://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css' />
			<link href='http://fonts.googleapis.com/css?family=Arvo' rel='stylesheet' type='text/css'>
            $head
          </head>
          <body>
            <div id='wrapper'>
	<div id='header-wrapper'>
		<div id='header'>
			<div id='logo'>
				<h1><a href='index.php'>Wibjers</a></h1>
				<p>PHP blog by Wibjer</p>
			</div>
		</div>
	</div>
	<!-- end #header -->
	<div id='menu-wrapper'>
		<div id='menu'>
			<ul>
				<li class='current_page_item'><a href='index.php'>Homepage</a></li>
				 $link
				<li><a href='About.html'>Om</a></li>
			</ul>
		</div>
	</div>
	<!-- end #menu -->
	<div id='page'>
		<div id='page-bgtop'>
			<div id='page-bgbtm'>
				<div id='page-content'>
					<div id='content'>
						$body
						<div style='clear: both;'>&nbsp;</div>
					</div>
					<!-- end #content -->
					<div id='sidebar'>
						<ul>
							<li>
								$login
							</li>
							<li>
								<h2>Tweetroll</h2>
                <a class='twitter-timeline' href='https://twitter.com/Wibjer' data-widget-id='264002842690453504'>Tweets av @Wibjer</a>
                <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js
                .src='//platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document,'script','twitter-wjs');</script>
						</ul>
					</div>
					<!-- end #sidebar -->
				</div>
				<div style='clear: both;'>&nbsp;</div>
			</div>
		</div>
	</div>
	<!-- end #page -->
</div>
<div id='footer'>
	<p>Copyright (c) 2012 wibjers.com. All rights reserved.</p>
</div>
<!-- end #footer -->
          </body>
        </html>";
        
    return $html;
  }
  
  /**
  * Returns a XHTML page
  * @param string $title  
  * @param string $body    
  * @return string     
  **/
  public function GetXHTML10StrictPage($title, $body) {
    $head = $this->BuildHeadTags(true);
    $xml = "
        <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> 
        <html xmlns=\"http://www.w3.org/1999/xhtml\"> 
          <head> 
             <title>$title</title> 
             <meta http-equiv=\"content-type\" content=\"text/html; charset=$this->m_charset\" /> 
             $head
          </head> 
          <body>
            $body
          </body>
        </html>";
    return $xml;
  }
}